package Engine.Module
{
	import Engine.Manager.EngineManager;
	
	import Interface.Module.IModuleBase;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;

	public class ModuleBase implements IModuleBase
	{
		private var _isShow:Boolean = false;
		private var _viewEnabled:Boolean = false;
		private var _moudleName:String = "";
		protected var _view:ViewBase = null;
		
		public function ModuleBase()
		{
			
		}
		
		public function show(p_parent:DisplayObjectContainer):void
		{
			if(_isShow)
				return;
			_isShow = true;
			if(_viewEnabled)
				onShowModule(p_parent);
			else
			{
				EngineManager.loaderMgr.loadSWF(_moudleName,onLoadOver,onProgress);
				EngineManager.loaderMgr.startloading();
			}
				
			function onLoadOver(p_e:Event,path:String):void
			{
//				_newModule(p_e,path);
//				imd = getModule(moduleName);
//				if(imd == null)
//				{
//					EngineManager.logPrint.printLog("path module haven't instance:",path);
//					return;
//				}
				onShowModule(p_parent);
			}
		}
		
		private function onProgress(e:ProgressEvent,path:String):void
		{
			
		}
		
		public function setMoudleName(mname:String):void
		{
			_moudleName = mname;
		}
		
		private function onShowModule(p_parent:DisplayObjectContainer):void
		{
			onShowBackPre();
			_view.show(p_parent);
			onShowBack();
		}
		
		public function dispose():void
		{
			if(_viewEnabled ==  false)
				return;
			if(_isShow)
			{
				_isShow = false;
				onDisposeBack();
				_view.dispose();
				onDisposeBackEnd();
			}
		}
		
		public function setView(view:ViewBase):void
		{
			_view = view;
			_viewEnabled = true;
		}
		
		public function get viewEnabled():Boolean
		{
			return _viewEnabled;
		}
		
		public function get isShow():Boolean
		{
			return this._isShow;
		}
		
		protected function printLog(...msg):void
		{
			EngineManager.logPrint.printLog(msg.join(" "));
		}
		
		protected function sendMsg(msgID:int,data:*):void
		{
			EngineManager.socketMgr.sendMessageData(msgID,data);
		}
		
		protected function onShowBackPre():void
		{
			
		}
		
		protected function onDisposeBack():void
		{
			
		}
		protected function onShowBack():void
		{
			
		}
		
		protected function onDisposeBackEnd():void
		{
			
		}
	}
}